﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using WebSiteAdminTool.UI.MembershipService;
using WebSiteAdminTool.UI.Utils;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using WebSiteAdminTool.UI.Utils.Commands;
using GalaSoft.MvvmLight.Messaging;
using WebSiteAdminTool.UI.ViewModels.Base;
using WebSiteAdminTool.UI.Services.Messaging;

namespace WebSiteAdminTool.UI.ViewModels.Security.Users
{
    public class UserListFilterViewModel : BaseViewModel
    {
        #region private data

        private ConstantsUserSearchCriteria _selectedFilterCriteria;
        private IEnumerable<ConstantsUserSearchCriteria> _filterCriteriaList = null;
        private string _filterValue = null;
        private GenericCommand _filterApplied = null;

        #endregion

        #region ctor

        public UserListFilterViewModel()
        {
            base.InitViewModel();
        }

        #endregion

        #region initialization

        protected override void SetDefaultValues()
        {
            base.SetDefaultValues();

            // default values
            SelectedFilterCriteria = ConstantsUserSearchCriteria.None;
            FilterCriteriaList = Extensions.GetValues<ConstantsUserSearchCriteria>();
            FilterValue = string.Empty;
        }

        protected override void InitializeCommands()
        {
            _filterApplied = new GenericCommand((obj) => MessageManager.Notify(CommandsEnum.UserListFilterApplied), true);

            base.InitializeCommands();
        }

        #endregion

        #region public props

        public ConstantsUserSearchCriteria SelectedFilterCriteria
        {
            get { return _selectedFilterCriteria; }
            set
            {
                _selectedFilterCriteria = value;
                EnableFiltering(_selectedFilterCriteria != ConstantsUserSearchCriteria.None);
                RaisePropertyChanged("SelectedFilterCriteria");
            }
        }

        public IEnumerable<ConstantsUserSearchCriteria> FilterCriteriaList
        {
            get { return _filterCriteriaList; }
            private set
            {
                _filterCriteriaList = value;
                RaisePropertyChanged("FilterCriteriaList");
            }
        }
        public string FilterValue
        {
            get { return _filterValue; }
            set
            {
                _filterValue = value;
                RaisePropertyChanged("FilterValue");
            }
        }

        // commands
        public GenericCommand FilterApplied
        {
            get
            {
                return _filterApplied;
            }
        }

        #endregion

        #region helpers

        private void EnableFiltering(bool enable)
        {
            if (!enable)
            {
                // reset target data grid when filter is disabled
                if (!IsInitializing)
                {
                    FilterApplied.Execute(null);
                }
                FilterValue = string.Empty;
            }
            FilterApplied.IsEnabled = enable;
        }

        #endregion
    }
}
